﻿Imports EstoqueDAL
Public Class ItensCompraBLL
    Dim objDAL As ItensCompraDAL
#Region "Propriedades"
    Property com_codigo As Integer
    Property itc_custoreal As Decimal
    Property itc_frete As Decimal
    Property itc_icms As Decimal
    Property itc_ipi As Decimal
    Property itc_margcontrib As Decimal
    Property itc_porcentagem As Decimal
    Property itc_qtde As Integer
    Property itc_substtrib As Decimal
    Property itc_valortotal As Decimal
    Property itc_valorunitario As Decimal
    Property pro_codigo As Integer
    Property itc_desconto As Decimal
    Property itc_precovenda As Decimal
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(com_codigo As Integer, itc_custoreal As Decimal, itc_frete As Decimal, itc_icms As Decimal, itc_ipi As Decimal, itc_margcontrib As Decimal, itc_porcentagem As Decimal, itc_qtde As Integer, itc_substtrib As Decimal, itc_valortotal As Decimal, itc_valorunitario As Decimal, pro_codigo As Integer, itc_desconto As Decimal, itc_precovenda As Decimal)
        Me.com_codigo = com_codigo
        Me.itc_custoreal = itc_custoreal
        Me.itc_frete = itc_frete
        Me.itc_icms = itc_icms
        Me.itc_ipi = itc_ipi
        Me.itc_margcontrib = itc_margcontrib
        Me.itc_porcentagem = itc_porcentagem
        Me.itc_qtde = itc_qtde
        Me.itc_substtrib = itc_substtrib
        Me.itc_valortotal = itc_valortotal
        Me.itc_valorunitario = itc_valorunitario
        Me.pro_codigo = pro_codigo
        Me.itc_desconto = itc_desconto
        Me.itc_precovenda = itc_precovenda
    End Sub
#End Region

#Region "Funções"
#Region "Inserir"
    Sub inserir()
        Try
                objDAL = New ItensCompraDAL
            objDAL.inserir(Me.com_codigo, Me.itc_custoreal, Me.itc_frete, Me.itc_icms, Me.itc_ipi, Me.itc_margcontrib, Me.itc_porcentagem, Me.itc_qtde, Me.itc_substtrib, Me.itc_valortotal, Me.itc_valorunitario, Me.pro_codigo, Me.itc_desconto, Me.itc_precovenda)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub

#End Region

#Region "Excluir"
    Sub excluir()
        Dim ret As Integer
        Try
            ret = -1
            objDAL = New ItensCompraDAL
                ret = objDAL.excluirtodositens(Me.com_codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Localizar"
    Public Function localizar(codigo As String, tipo As Integer) As DataTable
        Try
            objDAL = New ItensCompraDAL
            Return objDAL.buscar(codigo, tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region

#Region "Validar"
    Public Function validarcampos() As Boolean
        Dim objp As New PECABLL
        objp = objp.carregaproduto(Me.pro_codigo)
        If (Me.itc_qtde <= 0) Then Throw New Exception("Insira uma quantidade à peça " & objp.pro_descricao)
        If (Me.itc_valorunitario <= 0) Then Throw New Exception("Insira um valor unitário à peça " & objp.pro_descricao)
        If (Me.itc_valortotal <= 0) Then Throw New Exception("Verifique o valor total da peça " & objp.pro_descricao)
        'If (Me.itc_substtrib <= 0) Then Throw New Exception("Preencha o icms st da peça " & objp.pro_descricao)
        'If (Me.itc_ipi <= 0) Then Throw New Exception("Preencha o ipi da peça " & objp.pro_descricao)
        'If (Me.itc_frete <= 0) Then Throw New Exception("Verifique o frete da peça " & objp.pro_descricao)
        If (Me.itc_custoreal <= 0) Then Throw New Exception("Verifique o cálculo do custo real da peça " & objp.pro_descricao)
        If (Me.itc_margcontrib <= 0) Then Throw New Exception("Verifique o MC da peça " & objp.pro_descricao)
        If (Me.itc_porcentagem <= 0) Then Throw New Exception("Verifique a margem da peça " & objp.pro_descricao)
        If (Me.itc_precovenda <= 0) Then Throw New Exception("Verifique o preço de venda da peça " & objp.pro_descricao)
        Return True
    End Function
#End Region

#Region "Validar frete"
    Public Function validarfrete(valor As Decimal, frete As Decimal) As Boolean
        Dim objp As New PECABLL
        objp = objp.carregaproduto(Me.pro_codigo)
        If (Math.Round((Me.itc_frete * valor) / 100, 1) <> Math.Round(frete, 1)) Then Throw New Exception("O campo frete da peça não equivale ao percentual correto, por favor verifique a peça " & objp.pro_descricao)
        Return True
    End Function
#End Region
#End Region

#Region "Procedimentos"
#Region "Limpa Propriedades"
    Sub limpar()
        Me.com_codigo = Nothing
        Me.itc_custoreal = Nothing
        Me.itc_frete = Nothing
        Me.itc_icms = Nothing
        Me.itc_ipi = Nothing
        Me.itc_margcontrib = Nothing
        Me.itc_porcentagem = Nothing
        Me.itc_qtde = Nothing
        Me.itc_substtrib = Nothing
        Me.itc_valortotal = Nothing
        Me.itc_valorunitario = Nothing
        Me.pro_codigo = Nothing
        Me.objDAL = Nothing
    End Sub
#End Region
#End Region
End Class
